from rest_framework import serializers

from question.models import Question, Label, Reply
from users.models import User


class QuestionsIdLabelNewLabelsSerializer(serializers.ModelSerializer):
    """问题所属标签"""
    class Meta:
        model = Label
        fields = ('label_name', )


# 最新回答的问题
class QuestionsIdLabelNewSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(label='用户')
    labels = QuestionsIdLabelNewLabelsSerializer(many=True)

    class Meta:
        model = Question
        fields = '__all__'


# 评论问题用户
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')


# 回答评论列表
class SubsSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Reply
        fields = '__all__'


# 问题评论列表
class CommentQuestionSerializer(serializers.ModelSerializer):
    subs = SubsSerializer(many=True)
    user = UserSerializer()

    class Meta:
        model = Reply
        fields = ('id', 'content', 'createtime', 'useful_count', 'problem', 'unuseful_count', 'parent', 'subs', 'user')


# 问题回答列表
class AnswerQuestionSerializer(serializers.ModelSerializer):
    # 回答评论列表
    subs = SubsSerializer(many=True)
    user = UserSerializer()

    class Meta:
        model = Reply
        fields = ('id', 'content', 'createtime', 'useful_count', 'problem', 'unuseful_count', 'parent', 'subs', 'user')


# 问题详情
class QuestionsSerializer(serializers.ModelSerializer):
    labels = QuestionsIdLabelNewLabelsSerializer(many=True)
    user = serializers.StringRelatedField()

    class Meta:
        model = Question
        fields = '__all__'

